import { lastValueFrom } from "rxjs";
import { Injectable } from "@nestjs/common";

import {
  Book,
  RestDto,
  ENUM_APP,
  PageResult,
  putBookMapRpcDto,
  findManyBookRpcDto,
  insertManyBookRpcDto,
  doBusyCheckRpcDto,
  BookRpcFindManyPath,
  BookRpcInsertManyPath,
  BookMapRpcPutPath,
  deleteBookByTradeIDsRpcDto,
  deleteBookByCoupleIDsRpcDto,
  BookRpcInterfaceWithDerive,
  BookRpcDeleteByTradeIDsPath,
  BookMapRpcSyncQueuePath,
  BookRpcDeleteByCoupleIDsPath,
  BookSummaryRpcInterfaceWithDerive,
} from "qqlx-core";

import { BusRpc } from "./app.droplet-bus";
import { RpcWaiting } from "./rpc.response";
import { RpcConnection } from "./com.connection";

@Injectable()
export class BookRpc extends RpcConnection implements BookRpcInterfaceWithDerive<RestDto>, BookSummaryRpcInterfaceWithDerive<RestDto> {
  constructor(readonly BusRpc: BusRpc) {
    super(BusRpc, ENUM_APP.POND_BOOK);
  }

  @RpcWaiting(BookRpcDeleteByTradeIDsPath)
  deleteByTradeIDs(dto: RestDto & deleteBookByTradeIDsRpcDto) {
    const ob = this.CLIENT.send(BookRpcDeleteByTradeIDsPath, dto);
    return lastValueFrom<PageResult<Book>>(ob);
  }

  @RpcWaiting(BookRpcDeleteByCoupleIDsPath)
  deleteByCoupleIDs(dto: RestDto & deleteBookByCoupleIDsRpcDto) {
    const ob = this.CLIENT.send(BookRpcDeleteByCoupleIDsPath, dto);
    return lastValueFrom<PageResult<Book>>(ob);
  }

  @RpcWaiting(BookRpcFindManyPath)
  findMany(dto: RestDto & findManyBookRpcDto) {
    const ob = this.CLIENT.send(BookRpcFindManyPath, dto);
    return lastValueFrom<PageResult<Book>>(ob);
  }

  @RpcWaiting(BookRpcInsertManyPath, { logger: true })
  insertMany(dto: RestDto & insertManyBookRpcDto) {
    const ob = this.CLIENT.send(BookRpcInsertManyPath, dto);
    return lastValueFrom<Book[]>(ob);
  }

  /********** BookSummary ************/
  /********** BookSummary ************/
  /********** BookSummary ************/
  /********** BookSummary ************/
  /********** BookSummary ************/
  /********** BookSummary ************/
  /********** BookSummary ************/
  /********** BookSummary ************/

  @RpcWaiting(BookMapRpcSyncQueuePath)
  async doBusyCheck(dto: RestDto & doBusyCheckRpcDto) {
    const ob = this.CLIENT.send(BookMapRpcSyncQueuePath, dto);
    return lastValueFrom<null>(ob);
  }

  @RpcWaiting(BookMapRpcPutPath, { logger: true })
  async putBookMap(dto: RestDto & putBookMapRpcDto) {
    const ob = this.CLIENT.send(BookMapRpcPutPath, dto);
    return lastValueFrom<null>(ob);
  }
}
